const HOST = require('../../config.js').host

var app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isHaveCoupons: false,
    activeShoppingList:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let banner = JSON.parse(options.obj);
    wx.setNavigationBarTitle({
      title: banner.bannerName,
    })
  
      this.getActiveShoppingList(banner.id);
      this.judgeDiscountCoupon(banner.id);



  },
  /**
   * 判断用户是否领过优惠券
   */
  judgeDiscountCoupon(bannerId) {

    let self = this;
    var haveCoupons = self.data.isHaveCoupons;
    console.log(app.userId);
    wx.request({
      url: HOST + 'v1/usercoupons/checkIsHave/' + app.userId + ',' + bannerId,
      method: 'GET',
      success(res) {
        
        if (res.data.sysCode === 1) {
          haveCoupons = res.data.data;
          self.setData({
            isHaveCoupons: haveCoupons,
          })
          if (haveCoupons === false)  // 用户未领过优惠券
          {
            self.getCoupons(bannerId);
          }
        }
      }
    })
  },

  /**
   * 领取优惠券
   */
  getCoupons(bannerId) {
    wx.request({
      url: HOST + 'v1/usercoupons/saveUserCoupons',
      method: 'POST',
      data: {
        userId: app.userId,
        bannerId: bannerId
      },
      success: function (res) {
        if (res.data.sysCode !== 1) {
          wx.showToast({
            title: '领取优惠券失败',
          })
        }
      }
    })
  },

  /**
   * 获取活动商品
   */
  getActiveShoppingList(bannerId){
    // console.log(this.data.isHaveCoupons);
    let self = this;
    wx.request({
      url: HOST + 'v1/activity/getListByBannerId/' + bannerId,
      method: 'GET',
      success: function (res) {
        if (res.data.sysCode === 1) {
          self.setData({
            activeShoppingList: res.data.data
          })
        }
      }
    })
  }, 
  goDetail(e) 
  {
    if (e.currentTarget.dataset.id == " ")
    {
      return;
    } else {
      wx.navigateTo({
        url: `../detail/detail?id=${e.currentTarget.dataset.id}`
      })
    }
    
    
   
     
    
    
  }
})